package com.aac.demo;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;



import android.util.AttributeSet;
import android.view.View;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;

import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;

import android.view.MotionEvent;


public class CanvasView extends View {
	private Bitmap mBitmap;
	private Canvas mCanvas;
	private Paint mBitmapPaint;
	public BrushWriter mBrushWrter;
	public CanvasView(Context c) {
		super(c);
		mBitmapPaint = new Paint(Paint.DITHER_FLAG);
		
	}
	public CanvasView(Context c,Canvas cvs) {
		super(c);
		mBitmapPaint = new Paint(Paint.DITHER_FLAG);		
		mCanvas=cvs;
	}
	 public CanvasView(Context context, AttributeSet attrs) {
	        super(context, attrs);
	    }

	    public CanvasView (Context c, AttributeSet atr, int defstyle){
	        super(c, atr,defstyle);
	    }
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		mCanvas = new Canvas(mBitmap);
		mBrushWrter =new BrushWriter(mCanvas);
		rePaint();
	}
public void rePaint(){
	System.out.println("repaint");
	this.mCanvas.drawColor(0xFFAAAAAA);
	int sw=this.getWidth();
	int sh=this.getHeight();
	if(sw>sh)
	{
		sh=this.getWidth();
		sw=this.getHeight();
	}	
	int y = sh / 12;
	int width = sh * 5 / 12;
	int x= (sw-width)/2;
	int height = width;// sw * 2 / 3;
	drowMRect(x, y, width);
	drowMRect(x,y+height,width);
	invalidate();
}
	public void drowMRect(int x, int y, int width) {
		Paint paint = new Paint();
		paint.setStyle(Style.STROKE);
		paint.setColor(Color.RED);
		paint.setStrokeWidth(2.5f);
		mCanvas.drawLine(x, y, x + width, y, paint);
		mCanvas.drawLine(x, y + width, x + width, y + width, paint);
		mCanvas.drawLine(x, y, x, y + width, paint);
		mCanvas.drawLine(x + width, y, x + width, y + width, paint);
		Paint fgPaintSel = new Paint();
		fgPaintSel.setARGB(255, 0, 0, 0);
		fgPaintSel.setStyle(Style.STROKE);
		fgPaintSel.setPathEffect(new DashPathEffect(new float[] { 5, 5 },0));
		mCanvas.drawLine(x, y, x + width, y + width, fgPaintSel);
		mCanvas.drawLine(x + width, y, x, y + width, fgPaintSel);
		mCanvas.drawLine(x, y + width / 2, x + width, y + width / 2,
				fgPaintSel);
		mCanvas.drawLine(x + width / 2, y, x + width / 2, y + width,
				fgPaintSel);
		BrushWriter.mPen=BitmapFactory.decodeResource(getResources(), R.drawable.spen);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(0xFFAAAAAA);
		
		canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		float sz=event.getSize();
		float pres=event.getPressure();
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			//touch_start(x, y);
			mBrushWrter.touch_start(x, y,sz, pres);
			invalidate();
			break;
		case MotionEvent.ACTION_MOVE:
			//touch_move(x, y);
			//mBrushWrter.touch_move1(x, y,sz, pres);
			mBrushWrter.touch_move2(x, y,sz, pres);
			invalidate();
			break;
		case MotionEvent.ACTION_UP:
			//touch_up();
			mBrushWrter.touch_up(x, y,sz, pres);
			invalidate();
			break;
		}
		return true;
	}


	public Bitmap getMBmp() {
		// mBitmap=mCanvas.
		return mBitmap;
	}
}
